1 % Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite
2 {\ttfamily \raggedright {
4 \mbox{}\textit{\textcolor{Brown
}{/*
}} \\
5 \mbox{}\textit{\textcolor{Brown
}{\ \ Returns\ true\ if\ point\ (x,\ y)\ lies\ inside\ (or\ in\ the\ border)
}} \\
6 \mbox{}\textit{\textcolor{Brown
}{\ \ of\ box\ defined\ by\ points\ (x0,\ y0)\ and\ (x1,\ y1).
}} \\
7 \mbox{}\textit{\textcolor{Brown
}{*/
}} \\
8 \mbox{}\textcolor{ForestGreen
}{bool
}\
\textbf{\textcolor{Black
}{point$
\_$in$
\_$box
}}\textcolor{BrickRed
}{(
}\textcolor{ForestGreen
}{double
}\ x
\textcolor{BrickRed
}{,
}\
\textcolor{ForestGreen
}{double
}\ y
\textcolor{BrickRed
}{,
} \\
9 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
\textcolor{ForestGreen
}{double
}\ x0
\textcolor{BrickRed
}{,
}\
\textcolor{ForestGreen
}{double
}\ y0
\textcolor{BrickRed
}{,
}\ \ \ \
\textcolor{ForestGreen
}{double
}\ x1
\textcolor{BrickRed
}{,
}\
\textcolor{ForestGreen
}{double
}\ y1
\textcolor{BrickRed
}{)
}\textcolor{Red
}{\
{} \\
10 \mbox{}\ \
\textbf{\textcolor{Blue
}{return
}}\
\textbf{\textcolor{Black
}{min
}}\textcolor{BrickRed
}{(
}x0
\textcolor{BrickRed
}{,
}\ x1
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{$<$=
}\ x\
\textcolor{BrickRed
}{\&\&
}\ x\
\textcolor{BrickRed
}{$<$=
}\
\textbf{\textcolor{Black
}{max
}}\textcolor{BrickRed
}{(
}x0
\textcolor{BrickRed
}{,
}\ x1
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{\&\&
}\
\textbf{\textcolor{Black
}{min
}}\textcolor{BrickRed
}{(
}y0
\textcolor{BrickRed
}{,
}\ y1
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{$<$=
}\ y\
\textcolor{BrickRed
}{\&\&
}\ y\
\textcolor{BrickRed
}{$<$=
}\
\textbf{\textcolor{Black
}{max
}}\textcolor{BrickRed
}{(
}y0
\textcolor{BrickRed
}{,
}\ y1
\textcolor{BrickRed
}{);
} \\
11 \mbox{}\textcolor{Red
}{\
}} \\
13 \mbox{}\textit{\textcolor{Brown
}{/*
}} \\
14 \mbox{}\textit{\textcolor{Brown
}{\ \ Finds\ the\ intersection\ between\ two\ segments\ (Not\ infinite\ lines!)
}} \\
15 \mbox{}\textit{\textcolor{Brown
}{\ \ Segment\
1\ goes\ from\ point\ (x0,\ y0)\ to\ (x1,\ y1).
}} \\
16 \mbox{}\textit{\textcolor{Brown
}{\ \ Segment\
2\ goes\ from\ point\ (x2,\ y2)\ to\ (x3,\ y3).
}} \\
18 \mbox{}\textit{\textcolor{Brown
}{\ \ Handles\ the\ case\ when\ the\
2\ segments\ are:
}} \\
19 \mbox{}\textit{\textcolor{Brown
}{\ \ *Parallel\ but\ don't\ lie\ on\ the\ same\ line\ (No\ intersection)
}} \\
20 \mbox{}\textit{\textcolor{Brown
}{\ \ *Parallel\ and\ both\ lie\ on\ the\ same\ line\ (Infinite\ intersections\ or\ no\ intersections)
}} \\
21 \mbox{}\textit{\textcolor{Brown
}{\ \ *Not\ parallel\ (One\ intersection\ or\ no\ intersections)
}} \\
23 \mbox{}\textit{\textcolor{Brown
}{\ \ Returns\ true\ if\ the\ segments\ do\ intersect\ in\ any\ case.
}} \\
24 \mbox{}\textit{\textcolor{Brown
}{*/
}} \\
25 \mbox{}\textcolor{ForestGreen
}{bool
}\
\textbf{\textcolor{Black
}{segment$
\_$segment$
\_$intersection
}}\textcolor{BrickRed
}{(
}\textcolor{ForestGreen
}{double
}\ x0
\textcolor{BrickRed
}{,
}\
\textcolor{ForestGreen
}{double
}\ y0
\textcolor{BrickRed
}{,
}\ \ \
\textcolor{ForestGreen
}{double
}\ x1
\textcolor{BrickRed
}{,
}\
\textcolor{ForestGreen
}{double
}\ y1
\textcolor{BrickRed
}{,
} \\
26 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
\textcolor{ForestGreen
}{double
}\ x2
\textcolor{BrickRed
}{,
}\
\textcolor{ForestGreen
}{double
}\ y2
\textcolor{BrickRed
}{,
}\ \ \
\textcolor{ForestGreen
}{double
}\ x3
\textcolor{BrickRed
}{,
}\
\textcolor{ForestGreen
}{double
}\ y3
\textcolor{BrickRed
}{)
}\textcolor{Red
}{\
{} \\
27 \mbox{}\textbf{\textcolor{RoyalBlue
}{\#ifndef
}}\ EPS \\
28 \mbox{}\textbf{\textcolor{RoyalBlue
}{\#define
}}\ EPS\
\textcolor{Purple
}{1e-9} \\
29 \mbox{}\textbf{\textcolor{RoyalBlue
}{\#endif
}} \\
31 \mbox{}\ \
\textcolor{ForestGreen
}{double
}\ t0\
\textcolor{BrickRed
}{=
}\
\textcolor{BrickRed
}{(
}y3
\textcolor{BrickRed
}{-
}y2
\textcolor{BrickRed
}{)*(
}x0
\textcolor{BrickRed
}{-
}x2
\textcolor{BrickRed
}{)-(
}x3
\textcolor{BrickRed
}{-
}x2
\textcolor{BrickRed
}{)*(
}y0
\textcolor{BrickRed
}{-
}y2
\textcolor{BrickRed
}{);
} \\
32 \mbox{}\ \
\textcolor{ForestGreen
}{double
}\ t1\
\textcolor{BrickRed
}{=
}\
\textcolor{BrickRed
}{(
}x1
\textcolor{BrickRed
}{-
}x0
\textcolor{BrickRed
}{)*(
}y2
\textcolor{BrickRed
}{-
}y0
\textcolor{BrickRed
}{)-(
}y1
\textcolor{BrickRed
}{-
}y0
\textcolor{BrickRed
}{)*(
}x2
\textcolor{BrickRed
}{-
}x0
\textcolor{BrickRed
}{);
} \\
33 \mbox{}\ \
\textcolor{ForestGreen
}{double
}\ det\
\textcolor{BrickRed
}{=
}\
\textcolor{BrickRed
}{(
}y1
\textcolor{BrickRed
}{-
}y0
\textcolor{BrickRed
}{)*(
}x3
\textcolor{BrickRed
}{-
}x2
\textcolor{BrickRed
}{)-(
}y3
\textcolor{BrickRed
}{-
}y2
\textcolor{BrickRed
}{)*(
}x1
\textcolor{BrickRed
}{-
}x0
\textcolor{BrickRed
}{);
} \\
34 \mbox{}\ \
\textbf{\textcolor{Blue
}{if
}}\
\textcolor{BrickRed
}{(
}\textbf{\textcolor{Black
}{fabs
}}\textcolor{BrickRed
}{(
}det
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{$<$
}\ EPS
\textcolor{BrickRed
}{)
}\textcolor{Red
}{\
{} \\
35 \mbox{}\ \ \ \
\textit{\textcolor{Brown
}{//parallel
}} \\
36 \mbox{}\ \ \ \
\textbf{\textcolor{Blue
}{if
}}\
\textcolor{BrickRed
}{(
}\textbf{\textcolor{Black
}{fabs
}}\textcolor{BrickRed
}{(
}t0
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{$<$
}\ EPS\
\textcolor{BrickRed
}{$|$$|$
}\
\textbf{\textcolor{Black
}{fabs
}}\textcolor{BrickRed
}{(
}t1
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{$<$
}\ EPS
\textcolor{BrickRed
}{)
}\textcolor{Red
}{\
{} \\
37 \mbox{}\ \ \ \ \ \
\textit{\textcolor{Brown
}{//they\ lie\ on\ same\ line,\ but\ they\ may\ or\ may\ not\ intersect.
}} \\
38 \mbox{}\ \ \ \ \ \
\textbf{\textcolor{Blue
}{return
}}\
\textcolor{BrickRed
}{(
}\textbf{\textcolor{Black
}{point$
\_$in$
\_$box
}}\textcolor{BrickRed
}{(
}x0
\textcolor{BrickRed
}{,
}\ y0
\textcolor{BrickRed
}{,
}\ \ \ x2
\textcolor{BrickRed
}{,
}\ y2
\textcolor{BrickRed
}{,
}\ x3
\textcolor{BrickRed
}{,
}\ y3
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{$|$$|$
} \\
39 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ \ \
\textbf{\textcolor{Black
}{point$
\_$in$
\_$box
}}\textcolor{BrickRed
}{(
}x1
\textcolor{BrickRed
}{,
}\ y1
\textcolor{BrickRed
}{,
}\ \ \ x2
\textcolor{BrickRed
}{,
}\ y2
\textcolor{BrickRed
}{,
}\ x3
\textcolor{BrickRed
}{,
}\ y3
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{$|$$|$
} \\
40 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ \ \
\textbf{\textcolor{Black
}{point$
\_$in$
\_$box
}}\textcolor{BrickRed
}{(
}x2
\textcolor{BrickRed
}{,
}\ y2
\textcolor{BrickRed
}{,
}\ \ \ x0
\textcolor{BrickRed
}{,
}\ y0
\textcolor{BrickRed
}{,
}\ x1
\textcolor{BrickRed
}{,
}\ y1
\textcolor{BrickRed
}{)
}\
\textcolor{BrickRed
}{$|$$|$
} \\
41 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ \ \
\textbf{\textcolor{Black
}{point$
\_$in$
\_$box
}}\textcolor{BrickRed
}{(
}x3
\textcolor{BrickRed
}{,
}\ y3
\textcolor{BrickRed
}{,
}\ \ \ x0
\textcolor{BrickRed
}{,
}\ y0
\textcolor{BrickRed
}{,
}\ x1
\textcolor{BrickRed
}{,
}\ y1
\textcolor{BrickRed
}{));
} \\
42 \mbox{}\ \ \ \
\textcolor{Red
}{\
}}\textbf{\textcolor{Blue
}{else
}}\textcolor{Red
}{\
{} \\
43 \mbox{}\ \ \ \ \ \
\textit{\textcolor{Brown
}{//just\ parallel,\ no\ intersection
}} \\
44 \mbox{}\ \ \ \ \ \
\textbf{\textcolor{Blue
}{return
}}\
\textbf{\textcolor{Blue
}{false
}}\textcolor{BrickRed
}{;
} \\
45 \mbox{}\ \ \ \
\textcolor{Red
}{\
}} \\
46 \mbox{}\ \
\textcolor{Red
}{\
}}\textbf{\textcolor{Blue
}{else
}}\textcolor{Red
}{\
{} \\
47 \mbox{}\ \ \ \ t0\
\textcolor{BrickRed
}{/=
}\ det
\textcolor{BrickRed
}{;
} \\
48 \mbox{}\ \ \ \ t1\
\textcolor{BrickRed
}{/=
}\ det
\textcolor{BrickRed
}{;
} \\
49 \mbox{}\ \ \ \
\textit{\textcolor{Brown
}{/*
}} \\
50 \mbox{}\textit{\textcolor{Brown
}{\ \ \ \ \ \
0\ $<$=\ t0\ $<$=\
1\ iff\ the\ intersection\ point\ lies\ in\ segment\
1.
}} \\
51 \mbox{}\textit{\textcolor{Brown
}{\ \ \ \ \ \
0\ $<$=\ t1\ $<$=\
1\ iff\ the\ intersection\ point\ lies\ in\ segment\
2.
}} \\
52 \mbox{}\textit{\textcolor{Brown
}{\ \ \ \ */
}} \\
53 \mbox{}\ \ \ \
\textbf{\textcolor{Blue
}{if
}}\
\textcolor{BrickRed
}{(
}\textcolor{Purple
}{0.0}\
\textcolor{BrickRed
}{$<$=
}\ t0\
\textcolor{BrickRed
}{\&\&
}\ t0\
\textcolor{BrickRed
}{$<$=
}\
\textcolor{Purple
}{1.0}\
\textcolor{BrickRed
}{\&\&
}\
\textcolor{Purple
}{0.0}\
\textcolor{BrickRed
}{$<$=
}\ t1\
\textcolor{BrickRed
}{\&\&
}\ t1\
\textcolor{BrickRed
}{$<$=
}\
\textcolor{Purple
}{1.0}\textcolor{BrickRed
}{)
}\textcolor{Red
}{\
{} \\
54 \mbox{}\ \ \ \ \ \
\textcolor{ForestGreen
}{double
}\ x\
\textcolor{BrickRed
}{=
}\ x0\
\textcolor{BrickRed
}{+
}\ t0
\textcolor{BrickRed
}{*(
}x1
\textcolor{BrickRed
}{-
}x0
\textcolor{BrickRed
}{);
} \\
55 \mbox{}\ \ \ \ \ \
\textcolor{ForestGreen
}{double
}\ y\
\textcolor{BrickRed
}{=
}\ y0\
\textcolor{BrickRed
}{+
}\ t0
\textcolor{BrickRed
}{*(
}y1
\textcolor{BrickRed
}{-
}y0
\textcolor{BrickRed
}{);
} \\
56 \mbox{}\ \ \ \ \ \
\textit{\textcolor{Brown
}{//intersection\ is\ point\ (x,\ y)
}} \\
57 \mbox{}\ \ \ \ \ \
\textbf{\textcolor{Blue
}{return
}}\
\textbf{\textcolor{Blue
}{true
}}\textcolor{BrickRed
}{;
} \\
58 \mbox{}\ \ \ \
\textcolor{Red
}{\
}} \\
59 \mbox{}\ \ \ \
\textit{\textcolor{Brown
}{//the\ intersection\ points\ doesn't\ lie\ on\ both\ segments.
}} \\
60 \mbox{}\ \ \ \
\textbf{\textcolor{Blue
}{return
}}\
\textbf{\textcolor{Blue
}{false
}}\textcolor{BrickRed
}{;
} \\
61 \mbox{}\ \
\textcolor{Red
}{\
}} \\
62 \mbox{}\textcolor{Red
}{\
}} \\
64 } \normalfont\normalsize